package chatClient;

import chatClient.GUI.Chatroom;
import chatServer.ChatServiceRemoteImpl;
import chatServer.ChatServiceRemoteInterface;
import chatUniversal.InterfNotifier;
import chatUniversal.Notifier;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author bartspiering
 */
public class ChatClient implements Runnable {
    private InterfNotifier notifier;
    
    private ChatServiceRemoteInterface remoteService;
    private String username;

    @Override
    public void run() {
        connect();
        initGUI();
        /*while (true) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(System.in));
            String message = "";
            try {
                message = reader.readLine();
            } catch (IOException ex) {
                Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (!message.isEmpty()) {
                try {
                    System.out.println(username + ": " + remoteService.sendMessage(message, username));
                } catch (IOException ex) {
                    Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }*/
    }

    /**
     * the connect method starts the client and makes connection to the server with the desired username
     */
    public void connect() {
        username = JOptionPane.showInputDialog("Welke naam wil je gebruiken?");
        try {
            remoteService = (ChatServiceRemoteInterface) Naming.lookup("rmi://145.92.72.198/ChatService");
            
            //String welcome = remoteService.sendMessage(username + " is erbij gekomen", username);
            //remoteService.sendMessage("supershit", username);
            //System.out.println(welcome);
        } catch (NotBoundException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * initGUI initializes the GUI for the viewing of the chatroom
     */
    public void initGUI() {
        JFrame room = new JFrame();
        Chatroom jPanel = new Chatroom(username, this.remoteService);
        room.add(jPanel);
        room.setSize(400, 600);
        room.setVisible(true);
        try {
            notifier = new Notifier(jPanel);
            Naming.rebind(username, notifier);
            
            remoteService.registerClient(username, java.net.InetAddress.getLocalHost().getHostAddress());
            
        } catch (java.net.UnknownHostException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ChatServiceRemoteImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Notifier.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * main method, running starts here
     * @param args
     */
    public static void main(String[] args) {
        ChatClient chatClient = new ChatClient();
        Thread client = new Thread(chatClient);
        client.start();
    }
}
